package com.apollow.oschina.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.apollow.oschina.R;
import com.apollow.oschina.base.Global;
import com.apollow.oschina.bean.GeneralNewsBean;
import com.apollow.oschina.bean.GeneralNewsDetailBean;
import com.apollow.oschina.protocol.NewsProtocol;
import com.apollow.oschina.ui.WebViewActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/1/18.
 * 项目名:    OsChina
 * 包名:      com.apollow.oschina.adapter
 * 文件名:    NewsRvAdapter
 * 创建者:    LXF
 * 创建时间:   ${DATA}0:34
 * 描述:     TODO
 */

public class NewsRvAdapter extends RecyclerView.Adapter {
    public List<GeneralNewsBean.NewsBean> mlist = new ArrayList<>();
    private NewsProtocol mProtocol;
    private Activity mActivity;

    public NewsRvAdapter(Activity activity, NewsProtocol protocol) {
        this.mActivity = activity;
        this.mProtocol = protocol;
    }

    public void setData(List<GeneralNewsBean.NewsBean> list) {
        mlist.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = Global.inflate(R.layout.item_rv_news, parent);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder holderview = (ViewHolder) holder;
        holderview.setData(mlist.get(position));
    }
    @Override
    public int getItemCount() {
        return mlist == null ? 0 : mlist.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tv_title_synthesize)
        TextView mTvTitleSynthesize;
        @BindView(R.id.tv_content_synthesize)
        TextView mTvContentSynthesize;
        @BindView(R.id.tv_author_synthesize)
        TextView mTvAuthorSynthesize;
        @BindView(R.id.tv_data_synthesize)
        TextView mTvDataSynthesize;
        @BindView(R.id.tv_discuss_synthesize)
        TextView mTvDiscussSynthesize;
        public GeneralNewsBean.NewsBean newsBean;
        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                private TextView mTv1;
                private TextView mTv2;
                @Override
                public void onClick(View v) {
                    final String newsID=String.valueOf(newsBean.getId())+",";
                    String ReadItemId=Global.getReadedNews("");
                    mTv1 = (TextView) v.findViewById(R.id.tv_content_synthesize);
                    mTv2 = (TextView) v.findViewById(R.id.tv_title_synthesize);
                    if (!ReadItemId.contains(newsID)){
                        ReadItemId+=newsID;
                        Global.putReadedews(ReadItemId);
                        mTv1.setTextColor(Color.parseColor("#40AA53"));
                        mTv2.setTextColor(Color.parseColor("#40AA53"));
                    }
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Intent mIntent=new Intent(mActivity, WebViewActivity.class);
                            GeneralNewsDetailBean webViewURl = mProtocol.getWebViewURl(String.valueOf(newsBean.getId()));
                            mIntent.putExtra("URL", webViewURl.getNews().getUrl());
                            mActivity.startActivity(mIntent);
                        }
                    }
                    ).start();
                }
            });
        }
        public void setData(GeneralNewsBean.NewsBean newsBean) {
            this.newsBean = newsBean;
            mTvTitleSynthesize.setText(newsBean.getTitle());
            mTvContentSynthesize.setText(newsBean.getBody());
            mTvAuthorSynthesize.setText(newsBean.getAuthor());
            mTvDataSynthesize.setText(newsBean.getPubDate());
            mTvDiscussSynthesize.setText(newsBean.getCommentCount() + "");
            String ReadedItemID=Global.getReadedNews("");
            ReadedItemID.contains(String.valueOf(newsBean.getId()));
            if (!ReadedItemID.contains(String.valueOf(newsBean.getId()))){
                mTvTitleSynthesize.setTextColor(Color.parseColor("#352e2e"));
                mTvContentSynthesize.setTextColor(Color.parseColor("#352e2e"));
            }else {
                mTvTitleSynthesize.setTextColor(Color.parseColor("#40AA53"));
                mTvContentSynthesize.setTextColor(Color.parseColor("#40AA53"));
            }
        }
    }
}
